<?php
/**
 * 管理员操作记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog($log_info){
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = request()->ip();
    $add['log_url'] = request()->baseUrl() ;
    M('admin_log')->add($add);
}


function getAdminInfo($admin_id){
	return D('admin')->where("admin_id", $admin_id)->find();
}
 
/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin(){
    $navigate = include APP_PATH.'mingteng/conf/navigate.php';
    $location = strtolower('Admin/'.CONTROLLER_NAME);
    if($navigate[$location]){
        $con = strtolower(CONTROLLER_NAME) == 'index' ? 'javascript:void(0);' : U(CONTROLLER_NAME.'/'.key($navigate[$location]['action']));
        $arr = array(
            '后台首页'=>U('Index/welcome'),
            $navigate[$location]['name']=>$con,
            $navigate[$location]['action'][ACTION_NAME]=>'javascript:void(0);',
        );
        return $arr;
    }
}

/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename){
	header("Content-type: application/vnd.ms-excel");
	header("Content-Type: application/force-download");
	header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
	header('Expires:0');
	header('Pragma:public');
	echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
	$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
	for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
	return round($size, 2) . $delimiter . $units[$i];
}

//获取菜单列表
function getMenuArr(){
	$menuArr = include APP_PATH.'mingteng/conf/menu.php';
	$act_list = session('act_list');
	if($act_list != 'all' && !empty($act_list)){
		$right = M('system_menu')->where(array("id"=>array("in", $act_list)))->cache(true)->getField('right',true);
        $role_right = '';
		foreach ($right as $val){
			$role_right .= $val.',';
		}
        $role = explode(',', $role_right);
		foreach($menuArr as $k=>$val){
			foreach ($val['sub_menu'] as $j=>$v){
                if(!in_array($v['control'].'@'.$v['act'], $role)){
                    unset($menuArr[$k]['sub_menu'][$j]);//过滤菜单
                }
			}
		}
	}
	return $menuArr;
}

function geocoder($address){
    $config = config('mapsConfig');
    $url = "https://apis.map.qq.com/ws/geocoder/v1/?key=".$config['key']."&address=".$address;
    $res = urlGet($url);
    return $res;
}